package resources;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
/**
 * @web http://www.jc-mouse.net
 * @author Mouse
 */
public class CellRenderer extends DefaultTableCellRenderer {
 
    private String tipo="text";
    private final Font courier = new Font( "Courier New",Font.PLAIN ,12 );
    private final Font normal = new Font( "Arial",Font.PLAIN ,12 );
    private final Font bold = new Font( "Arial",Font.BOLD ,12 );
    private final JLabel label = new JLabel();
    private final ImageIcon salida = new ImageIcon(getClass().getResource("/imagenes/salida.jpg"));
    private final ImageIcon entrada = new ImageIcon(getClass().getResource("/imagenes/entrada.jpg"));
    private final ImageIcon reingreso = new ImageIcon(getClass().getResource("/imagenes/reingreso.jpg"));
    private final ImageIcon ajuste = new ImageIcon(getClass().getResource("/imagenes/ajuste.jpg"));
    private final ImageIcon transferencia = new ImageIcon(getClass().getResource("/imagenes/transferencia.jpg"));
    
    /** Constructor de clase
     * @param tipo String
     * Ej.: "text", "text center", "hour" , "num" , "icon"
     */
    public CellRenderer( String tipo )
    {
        this.tipo = tipo;
    }
    
    
    @Override
    public Component getTableCellRendererComponent ( JTable table, Object value, boolean selected, boolean focused, int row, int column )
    {   
        if (selected) {                
            this.setBackground( new Color( 50, 153 , 254) );                        
        }
        else
        {
            this.setBackground(Color.white);
        }
        
         if( tipo.equals("antes"))
        {
            this.setHorizontalAlignment( JLabel.CENTER );
            this.setText( String.valueOf(value) );
            this.setForeground( (selected)?new Color(255,255,255):new Color(0,0,0) );            
            this.setFont(bold);            
            return this;
        }
                
        if( tipo.equals("cantidad")&&value!=null)
        {           
           this.setHorizontalAlignment( JLabel.CENTER );
            this.setText( String.valueOf(value) );            
            this.setForeground( (Float.valueOf(String.valueOf( value))<0.0f)?Color.RED:new Color(0,150,0) );            
            this.setFont(bold);            
            return this;   
        }        
        
        if( tipo.equals("icon"))
        {
            if( String.valueOf(value).equals("Ingreso") )
            {
                label.setIcon(entrada);
            }
            else if( String.valueOf(value).equals("Ajuste") )
            {
                label.setIcon(ajuste);
            }
            else if( String.valueOf(value).equals("Salida") )
            {
                label.setIcon(salida);
            }
              else if( String.valueOf(value).equals("Reingreso") )
            {
                label.setIcon(reingreso);
            }
            else if( String.valueOf(value).equals("Transferencia") )
            {
                label.setIcon(transferencia);
            }
            return label;
        }
        
        return this;
    }
}
